module(..., package.seeall)

require "misc"
require "net"
require "myUiWin"
require "sim"

require "tool"
require "config"
require "aLiIotda"

-- PAGEID：窗口id
PAGEID = 0

local showTimeSplit = false -- 通过轮替显示logo
local backColor = 0x00

local time_count = 0

local page_turn = 0

local show_first_key = 1 -- 当前显示菜单页面第一行 对应table里面的一级菜单索引key值

local data_pub = false


local sig = 0
local motor = 0
local motor2 = 0
local run_count = 0
local weight_count_1 = 0
local weight_count_2 = 0

local function getPowerState(state) power_state = state end
local function getSig(s) sig = s end
local function getMotor(s) motor = tonumber(s) end
local function getMotor2(s) motor2 = tonumber(s) end
local function getWeight(t, s, x)
    if tonumber(s) > 0 then weight_count_1 = tonumber(s) end
    if tonumber(x) > 0 then weight_count_2 = tonumber(x) end
    if tonumber(t) > 0 then run_count = tonumber(t) end
end

-- 画电源状态
local function powerDraw()
    if power_state == 1 then disp.putimage("/lua/poweroff.png", 152, 3) end
end

local function drawDevice()
    if sig == 0 then
        disp.putimage("/lua/sig_on.png", 8, 85)
    else
        disp.putimage("/lua/sig_off.png", 8, 85)
    end

    if motor ~= nil and motor2 ~= nil and ((motor2 > 95 and motor2 <= 100) or (motor > 95 and motor <= 100)) then
        disp.putimage("/lua/motor_on.png", 10, 35)
    else
        disp.putimage("/lua/motor_off.png", 10, 35)
    end

    if motor ~= nil and motor > 95 and motor <= 100 then
        disp.putimage("/lua/1.png", 40, 65)
    end

    if motor2 ~= nil and motor2 > 95 and motor2 <= 100 then
        disp.putimage("/lua/2.png", 48, 65)
    end
end

local function drawLine()
    disp.setcolor(0xFFFF)
    disp.setbkcolor(backColor)
    disp.clear()
    disp.drawrect(0, 22, 95, 24, 0x017E)
    disp.drawrect(105, 22, 160, 24, 0x06C0)
end

-- 画4G 信号图标
local function rssiDraw()
    local x, y = 137, 0
    local rssi = net.getRssi()
    local color = 0xFFFF
    --log.info("NETMODE", net.getNetMode())
    if net.getNetMode() == 0 then
        color = 0xD8E0
    else
        color = 0xFFFF
    end
    if sim.getStatus() ~= true then
        disp.putimage("/lua/noSim.png", 141, 0)
        return
    end
    -- 无信号时，画"- - -"
    if rssi == 0 then
        disp.drawrect(x + 1, y + 8, x + 5, y + 10, color)
        disp.drawrect(x + 6, y + 8, x + 10, y + 10, color)
        disp.drawrect(x + 11, y + 8, x + 15, y + 10, color)
        -- 画1格信号
    elseif rssi > 0 and rssi <= 13 then
        disp.drawrect(x + 2, y + 12, x + 4, y + 16, color)
        -- 画2格信号
    elseif rssi > 13 and rssi <= 17 then
        disp.drawrect(x + 2, y + 12, x + 4, y + 16, color)
        disp.drawrect(x + 6, y + 9, x + 8, y + 16, color)
        -- 画3格信号
    elseif rssi > 17 then
        disp.drawrect(x + 2, y + 12, x + 4, y + 16, color)
        disp.drawrect(x + 6, y + 9, x + 8, y + 16, color)
        disp.drawrect(x + 10, y + 5, x + 12, y + 16, color)
    end
end

-- 画实时时间
local function timeDraw()
    local tm = misc.getClock()
    if showTimeSplit == false then
        disp.puttext(string.format("%02d-%02d %02d %02d", tm.month, tm.day,
            tm.hour, tm.min), 4, 2)
        if time_count == 18 then
            showTimeSplit = true
            time_count = 0
        end
    else
        disp.puttext(string.format("%02d-%02d %02d:%02d", tm.month, tm.day,
            tm.hour, tm.min), 4, 2)
        if time_count == 18 then
            showTimeSplit = false
            time_count = 0
        end
    end
end

-- 画MQTT服务器链接状态
local function connectDraw()
    local x = 110
    local y = 3
    if nvm.get("HW_KEY") == "" then
        if aLiIotda.isReady() == true and socket.isReady() == true then
            if data_pub then
                disp.putimage("/lua/online2.png", x, y)
                data_pub = false
            else
                disp.putimage("/lua/online.png", x, y)
            end
        else
            disp.putimage("/lua/offline.png", x, y)
        end
    else
        if aLiIotda.isReady() == true and socket.isReady() == true and
            hwIotda.isReady() == true then
            if data_pub then
                disp.putimage("/lua/online2.png", x, y)
                data_pub = false
            else
                disp.putimage("/lua/online.png", x, y)
            end
        else
            disp.putimage("/lua/offline.png", x, y)
        end
    end
end

local function mainShow()
    disp.puttext(common.utf8ToGb2312("打料时间"), 70, 35)
    disp.puttext(string.format("%.1f", run_count) .. " s", 90, 55)
    disp.puttext(common.utf8ToGb2312("打料重量"), 70, 80)
    disp.setfontheight(22)
    local we = string.format("%.1f",
        weight_count_1 * nvm.get("WEIGHT_COEFFICIENT_1") + weight_count_2 * nvm.get("WEIGHT_COEFFICIENT_2"))
    disp.puttext(we .. " kg", 140 - string.len(we .. " kg") * 10, 100)
    disp.setfontheight(16)
end
-- 窗口初始化函数
function initPage()
    page_turn = 0
    show_first_key = 1
    local id = 1

    drawLine()
    rssiDraw()
    timeDraw()
    connectDraw()
    drawDevice()
    powerDraw()
    mainShow()
    disp.update()
end

-- 定时刷新函数
function refresh()
    drawLine()
    rssiDraw()
    timeDraw()
    connectDraw()
    drawDevice()
    powerDraw()
    mainShow()
    time_count = time_count + 1
    disp.update()
end

function onKey(value)
    if value == "enter" then
        myUiWin.openWin(ui_password.PAGEID, "index")
        return
    end
end

sys.subscribe("POWER_STATE", getPowerState)
sys.subscribe("SIG", getSig)
sys.subscribe("MOTOR", getMotor)
sys.subscribe("MOTOR2", getMotor2)
sys.subscribe("WEIGHT_COUNT", getWeight)
